﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gnosis.Alexandria.Model.Commands
{
    public class UpdateTextBuilder
        : CommandBuilderBase
    {
        public UpdateTextBuilder(IIdentifiable record)
            : base(record)
        {
            if (record.Id > 0)
            {
                var changes = record.Changes();
                if (changes.Count > 0)
                {
                    var values = new StringBuilder();

                    var delimiter = string.Empty;
                    foreach (KeyValuePair<string, object> change in changes)
                    {
                        var parameterName = GetParameterName(change.Key);
                        values.AppendFormat("{0}{1} = {2}", delimiter, change.Key, parameterName);
                        AddParameter(change.Key, change.Value);
                        delimiter = ",";
                    }

                    AppendFormat("UPDATE {0} SET {1}", record.RecordName, values);
                    AppendWhereClause(record.Id);
                }
            }
        }
    }
}
